﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Economo.Services.DataContext;
using Economo.Services.EntitiesConversion;

namespace Economo.Services.BusinessLogic
{
    public class BanksBusinessLogic
    {
        /// <summary>
        /// Gets a list of banks filtering by description 
        /// empty parameter will bring the whole list
        /// </summary>
        /// <param name="description"></param>
        /// <returns></returns>
        public List<BusinessEntities.Bank> GetBanksByDescription(string description)
        {
            BanksDataContext da = new BanksDataContext();
            try
            {
                List<DataContext.Bank> banks = da.GetBanksByDescription(description);
                List<BusinessEntities.Bank> result = Convertions.ConvertToBusinessEntity<BusinessEntities.Bank, Bank>(banks);
                return result;                
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }

        }



        public BusinessEntities.Bank GetBankById(byte id)
        {
            BanksDataContext da = new BanksDataContext();
            try
            {
                DataContext.Bank bank = da.GetBankById(id);
                BusinessEntities.Bank result = Convertions.ConvertToBusinessEntity<BusinessEntities.Bank>(bank);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }

        }

        public byte InsertBank(BusinessEntities.Bank newBank)
        {
            BanksDataContext da = new BanksDataContext();
            try
            {
                Bank bank = Convertions.ConvertToDataEntity<Bank>(newBank);
                newBank.Id = da.InsertBank(bank);
                return newBank.Id;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return 0;
            }
            finally
            {
                da.Dispose();
            }
        }

        public void UpdateBank(BusinessEntities.Bank updatedBank)
        {
            BanksDataContext da = new BanksDataContext();            
            try
            {
                Bank bank = Convertions.ConvertToDataEntity<Bank>(updatedBank);
                da.UpdateBank(bank);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }
        }

        public void DeleteBank(BusinessEntities.Bank bankToDelete)
        {
            BanksDataContext da = new BanksDataContext();
            try
            {
                Bank bank = Convertions.ConvertToDataEntity<Bank>(bankToDelete);
                da.DeleteBank(bank);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }

        }
        public void DeleteBank(byte id)
        {
            BanksDataContext da = new BanksDataContext();
            try
            {                
                da.DeleteBank(id);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }

        }

    }
}
